import 'package:flutter/material.dart';
import 'package:untitled/utils/SnackBarHepler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      scaffoldMessengerKey: ScaffoldMessengerGlobalKey,
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    //脚手架
    return Builder(
      builder: (BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('底部弹框'),
            backgroundColor: Colors.blue,
            foregroundColor: Colors.white,
          ),

          body: SizedBox.expand(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                TextButton(
                  onPressed: () {
                    test();
                  },
                  child: Text("显示弹框1"),
                ),
                TextButton(
                  onPressed: () {
                    test1();
                  },
                  child: Text("显示弹框2"),
                ),
                TextButton(
                  onPressed: () {
                    shareBottomSheet();
                  },
                  child: Text("分享"),
                ),
                TextButton(
                  onPressed: () {
                    inputBottomSheet();
                  },
                  child: Text("输入内容 "),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  TextEditingController textEditingController = TextEditingController();
  Future<void> inputBottomSheet() async {
    textEditingController.text = '';
    dynamic res = await showModalBottomSheet(
      context: context,
      enableDrag: true,
      showDragHandle: true,
      isDismissible: true,

      isScrollControlled: true,

      builder: (BuildContext context) {
        return SingleChildScrollView(
          //键盘遮挡问题
          padding: EdgeInsets.only(
            bottom: MediaQuery.of(context).viewInsets.bottom,
          ),
          child: Container(
            padding: EdgeInsets.all(20),
            width: double.infinity,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
            ),
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Text(
                    '任务列表',
                    style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextField(
                    controller: textEditingController,
                    decoration: InputDecoration(labelText: '输入内容 '),
                  ),
                ),
                SizedBox(height: 30),

                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    OutlinedButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: Text('取消'),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        String text = textEditingController.text;
                        Navigator.of(context).pop(text);
                      },
                      child: Text('确定 '),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );

    print('弹框关闭  $res');
  }

  void shareBottomSheet() {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return Container(
          padding: EdgeInsets.all(16),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ListTile(
                leading: Icon(Icons.share),
                title: Text('分享'),
                onTap: () {
                  Navigator.of(context).pop();
                },
              ),
              ListTile(
                leading: Icon(Icons.copy),
                title: Text('复制连接'),
                onTap: () {},
              ),
              ListTile(
                leading: Icon(Icons.delete),
                title: Text('删除'),
                onTap: () {},
              ),
            ],
          ),
        );
      },
    );
  }

  void test1() {
    // 显示底部面板
    final controller = showBottomSheet(
      context: context,
      builder: (context) => Container(
        height: 200,
        child: Column(
          children: [
            ListTile(leading: Icon(Icons.share), title: Text('分享')),
            ListTile(leading: Icon(Icons.download), title: Text('下载')),
            ListTile(leading: Icon(Icons.delete), title: Text('删除')),
          ],
        ),
      ),
    );
  }

  void test() {
    showModalBottomSheet(
      context: context,
      //弹框背景
      backgroundColor: Colors.red,
      //遮罩颜色
      barrierColor: Colors.black54,
      //false 点击遮罩层不关闭
      isDismissible: true,
      //false 禁止拖动关闭
      enableDrag: false,
      //显示拖拽手柄
      showDragHandle: true,
      //阴影高度
      elevation: 20,

      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20),
          topRight: Radius.circular(20),
        ),
      ),
      //
      isScrollControlled: true,

      builder: (BuildContext context) {
        return Container(
          width: double.infinity,
          child: Text('这里是弹框的内容'),
          color: Colors.transparent,
          height: 200,
        );
      },
    );
  }
}
